package com.pioneer.controller.sale;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.sale.domain.SalOrder;
import com.pioneer.web.sale.service.ISalOrderService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 销售订单
 *
 * @author hlm
 */
@RestController
@RequestMapping("/sale/order")
public class SalOrderController extends BaseController {

    @Resource
    private ISalOrderService orderService;

    /**
     * 获取销售订单列表
     *
     * @param order 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:order:list')")
    @GetMapping("/list")
    public AjaxResult list(SalOrder order) {
        boolean isPage = startPage();
        List<SalOrder> list = orderService.list(Wrappers.query(order));
        return success(isPage, list);
    }

    /**
     * 根据销售订单id获取详细信息
     *
     * @param orderId 销售订单id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable Long orderId) {
        return AjaxResult.success(orderService.getOrderById(orderId));
    }

    /**
     * 修改销售订单
     *
     * @param order 销售订单
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:order:edit')")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SalOrder order) {
        return toAjax(orderService.updateOrderById(order));
    }

    /**
     * 提交销售订单
     * @param orderIds 销售订单ID数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:order:commit')")
    @Log(title = "销售订单", businessType = BusinessType.OTHER)
    @PostMapping("/commit")
    public AjaxResult commit(@RequestBody Long[] orderIds) {
        return toAjax(orderService.commitOrder(Arrays.asList(orderIds)));
    }

    /**
     * 撤回销售订单
     * @param orderIds 销售订单ID数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:order:recall')")
    @Log(title = "销售订单", businessType = BusinessType.OTHER)
    @PostMapping("/recall")
    public AjaxResult recall(@RequestBody Long[] orderIds) {
        return toAjax(orderService.recallOrder(Arrays.asList(orderIds)));
    }

    /**
     * 审批销售订单
     * @param order 销售订单
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:order:approve')")
    @Log(title = "销售订单", businessType = BusinessType.OTHER)
    @PostMapping("/approve")
    public AjaxResult approve(@RequestBody SalOrder order) {
        return toAjax(orderService.approveOrder(order));
    }

    /**
     * 弃审销售订单
     * @param order 销售订单
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:order:unapprove')")
    @Log(title = "销售订单", businessType = BusinessType.OTHER)
    @PostMapping("/unapprove")
    public AjaxResult unapprove(@RequestBody SalOrder order) {
        return toAjax(orderService.unapproveOrder(order));
    }
}
